package com.android.settings.ios.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import com.android.settings.ios.OsUtils;
import com.android.settings.ios.R;

public class OsPreference extends Preference implements IOsPreference {

    private int mBgMode;
    private int mLeftActionResId = 0;
    private int mTitleTextColor = 0;
    private boolean isShowRightArrow;
    private boolean isInHighlight = false;
    private boolean isSelectedEffect = true;

    private String mSecondTextStr;
    private int mSecondTextColor;
    private Bundle mAppBundle = null;
    private Bitmap mIconBitmap = null;

    private View mMainView;
    private OnOsActionClickListener mListener;
    private Fragment mSelfFragment = null;

    public OsPreference(Context context) {
        this(context, null, 0, 0);
    }

    public OsPreference(Context context, boolean selectedEffect) {
        this(context, null, 0, 0);
        isSelectedEffect = selectedEffect;
    }

    public OsPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0, 0);
    }

    public OsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public OsPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.OsPreference, defStyleAttr, 0);

        mLeftActionResId = a.getResourceId(R.styleable.OsPreference_leftActionIcon, 0);
        isShowRightArrow = a.getBoolean(R.styleable.OsPreference_showRightArrow, true);
        mTitleTextColor = a.getColor(R.styleable.OsPreference_osTextColor, 0);
        mSecondTextStr = a.getString(R.styleable.OsPreference_secondaryText);
        mSecondTextColor = a.getColor(R.styleable.OsPreference_secondaryTextColor, 0);
        isSelectedEffect = a.getBoolean(R.styleable.OsPreference_selectedEffect, false);
        isInHighlight = a.getBoolean(R.styleable.OsPreference_selectedDefault, false);
    }

    @Override
    public void onAttached() {
        super.onAttached();
        setLayoutResource(R.layout.os_preference);
        setIconSpaceReserved(false);
        if (isSelectedEffect) {
            OsUtils.registerSelectEffect(mReceiver);
        }
    }

    @Override
    public void onDetached() {
        super.onDetached();
        if (isSelectedEffect) {
            OsUtils.unregisterSelectEffect(mReceiver);
        }
    }

    public Fragment getSelfFragment() {
        return mSelfFragment;
    }

    public void setSelfFragment(Fragment fragment) {
        mSelfFragment = fragment;
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        OsBackgroundUtil.updateBackground(mBgMode, holder.itemView);
        holder.itemView.setSelected(isInHighlight);
        mMainView = holder.itemView;

        ImageView icon = holder.itemView.findViewById(android.R.id.icon);
        if (icon != null) {
            if (mIconBitmap != null) {
                icon.setImageBitmap(mIconBitmap);
                icon.setVisibility(View.VISIBLE);
            }
        }

        ImageView leftIcon = holder.itemView.findViewById(android.R.id.icon1);
        if (leftIcon != null) {
            if (mLeftActionResId != 0) {
                leftIcon.setImageResource(mLeftActionResId);
                leftIcon.setVisibility(View.VISIBLE);
                if (mListener != null) {
                    leftIcon.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            mListener.onOsActionClicked(v);
                        }
                    });
                }
            } else {
                leftIcon.setVisibility(View.GONE);
            }
        }

        ImageView rightIcon = holder.itemView.findViewById(android.R.id.icon2);
        if (rightIcon != null) {
            rightIcon.setVisibility(isShowRightArrow ? View.VISIBLE : View.GONE);
        }

        TextView tvTitle = holder.itemView.findViewById(android.R.id.title);
        if (tvTitle != null) {
            if (mTitleTextColor != 0) {
                tvTitle.setTextColor(mTitleTextColor);
            }
        }

        TextView tvSecondary = holder.itemView.findViewById(android.R.id.text1);
        if (tvSecondary != null) {
            if (mSecondTextStr != null && mSecondTextStr.length() > 0) {
                tvSecondary.setText(mSecondTextStr);
                tvSecondary.setVisibility(View.VISIBLE);
                if (mSecondTextColor != 0) {
                    tvSecondary.setTextColor(mSecondTextColor);
                }
            } else {
                tvSecondary.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void setItemBackgroundMode(int mode) {
        mBgMode = mode;
    }

    @Override
    public Bundle getAppArgs() {
        return mAppBundle;
    }

    public void setIconBitmap(Bitmap bitmap) {
        mIconBitmap = bitmap;
    }

    public void setAppArgs(Bundle bundle) {
        mAppBundle = bundle;
    }

    public void setLeftActionIcon(int iconId) {
        mLeftActionResId = iconId;
    }

    public void setLeftActionClickListener(OnOsActionClickListener listener) {
        mListener = listener;
    }

    public void setShowRightArrow(boolean show) {
        isShowRightArrow = show;
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (OsUtils.ACTION_SELECT_MENU_CHANGED.equals(action)) {
                String key = intent.getStringExtra("key");
                if (key == null) {
                    CharSequence title = intent.getCharSequenceExtra("title");
                    isInHighlight = title != null && title.equals(getTitle());
                } else {
                    isInHighlight = key.equals(getKey());
                }
                if (mMainView != null) {
                    mMainView.setSelected(isInHighlight);
                }
            }
        }
    };

    public interface OnOsActionClickListener {

        void onOsActionClicked(View view);
    }
}
